package com.puzzletest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.PointF;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.puzzletest.bean.General;
import com.puzzletest.net.LoadData;
import com.puzzletest.net.Util;
import com.puzzletest.view.CustomImageView;

public class MoreActivity extends Activity {
	
	private final Activity activity = this;
	private ProgressDialog progress;

	private Button btn_save;
	private Button btn_recover;
	
	private CustomImageView imageView1;
	private CustomImageView imageView2;
	private CustomImageView imageView3;
	private CustomImageView imageView4;
	private CustomImageView imageView5;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.act_more);
		
		btn_save = (Button) findViewById(R.id.btn_save);
		btn_recover = (Button) findViewById(R.id.btn_recover);
		
		imageView1 = (CustomImageView) findViewById(R.id.imageView1);
		imageView2 = (CustomImageView) findViewById(R.id.imageView2);
		imageView3 = (CustomImageView) findViewById(R.id.imageView3);
		imageView4 = (CustomImageView) findViewById(R.id.imageView4);
		imageView5 = (CustomImageView) findViewById(R.id.imageView5);
		
		btn_save.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				showProgress();
				
				General g = LoadData.putImageInfoMore(getDataList());
				Message msg = new Message();
				msg.what = 1;
				msg.obj = g;
				mHandler.sendMessage(msg);
				
			}
		});
		
		btn_recover.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(activity, MoreRecoverActivity.class);
				startActivity(intent);
			}
		});
	}

	private Handler mHandler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case 1:
				hiddenProgress();
				General g = (General) msg.obj;
				Toast.makeText(activity, g.getMessage(), Toast.LENGTH_SHORT).show();
				
				break;

			default:
				break;
			}
		};
	};
	
	public List<Map<String, String>> getDataList(){
		List<Map<String, String>> datalist = new ArrayList<Map<String,String>>();
		
		PointF pointF1 = imageView1.getCustomRotationPoint();
		float[] points1 = imageView1.getCustomArrayPoint();
		String pointarray1 = "";
		for (float f : points1) {
			pointarray1 += String.valueOf(f)+",";
		}
		String rotationpoint1 = pointF1.x+","+pointF1.y;
		String scale1 = String.valueOf(imageView1.getCustomScale()); // 缩放比例
		String rotation1 = String.valueOf(imageView1.getCustomRotation());//旋转的角度
		pointarray1 = pointarray1.substring(0, pointarray1.length() -1);
		
		Map<String, String> map1 = new HashMap<String, String>();
		map1.put("scale", scale1);
		map1.put("rotation", rotation1);
		map1.put("rotationpoint", rotationpoint1);
		map1.put("pointarray", pointarray1);
		map1.put("name", "aa");
		datalist.add(map1);
		
		PointF pointF2 = imageView2.getCustomRotationPoint();
		float[] points2 = imageView2.getCustomArrayPoint();
		String pointarray2 = "";
		for (float f : points2) {
			pointarray2 += String.valueOf(f)+",";
		}
		String rotationpoint2 = pointF2.x+","+pointF2.y;
		String scale2 = String.valueOf(imageView2.getCustomScale()); // 缩放比例
		String rotation2 = String.valueOf(imageView2.getCustomRotation());//旋转的角度
		pointarray2 = pointarray2.substring(0, pointarray2.length() -1);
		
		Map<String, String> map2 = new HashMap<String, String>();
		map1.put("scale", scale2);
		map1.put("rotation", rotation2);
		map1.put("rotationpoint", rotationpoint2);
		map1.put("pointarray", pointarray2);
		map1.put("name", "bb");
		datalist.add(map2);
		
		PointF pointF3 = imageView1.getCustomRotationPoint();
		float[] points3 = imageView1.getCustomArrayPoint();
		String pointarray3 = "";
		for (float f : points3) {
			pointarray3 += String.valueOf(f)+",";
		}
		String rotationpoint3 = pointF3.x+","+pointF3.y;
		String scale3 = String.valueOf(imageView3.getCustomScale()); // 缩放比例
		String rotation3 = String.valueOf(imageView3.getCustomRotation());//旋转的角度
		pointarray3 = pointarray3.substring(0, pointarray3.length() -1);
		
		Map<String, String> map3 = new HashMap<String, String>();
		map1.put("scale", scale3);
		map1.put("rotation", rotation3);
		map1.put("rotationpoint", rotationpoint3);
		map1.put("pointarray", pointarray3);
		map1.put("name", "cc");
		datalist.add(map3);
		
		PointF pointF4 = imageView4.getCustomRotationPoint();
		float[] points4 = imageView4.getCustomArrayPoint();
		String pointarray4 = "";
		for (float f : points4) {
			pointarray4 += String.valueOf(f)+",";
		}
		String rotationpoint4 = pointF4.x+","+pointF4.y;
		String scale4 = String.valueOf(imageView4.getCustomScale()); // 缩放比例
		String rotation4 = String.valueOf(imageView4.getCustomRotation());//旋转的角度
		pointarray4 = pointarray4.substring(0, pointarray4.length() -1);
		
		Map<String, String> map4 = new HashMap<String, String>();
		map1.put("scale", scale4);
		map1.put("rotation", rotation4);
		map1.put("rotationpoint", rotationpoint4);
		map1.put("pointarray", pointarray4);
		map1.put("name", "dd");
		datalist.add(map4);
		
		PointF pointF5 = imageView5.getCustomRotationPoint();
		float[] points5 = imageView5.getCustomArrayPoint();
		String pointarray5 = "";
		for (float f : points5) {
			pointarray5 += String.valueOf(f)+",";
		}
		String rotationpoint5 = pointF5.x+","+pointF5.y;
		String scale5 = String.valueOf(imageView5.getCustomScale()); // 缩放比例
		String rotation5 = String.valueOf(imageView5.getCustomRotation());//旋转的角度
		pointarray5 = pointarray5.substring(0, pointarray5.length() -1);
		
		Map<String, String> map5 = new HashMap<String, String>();
		map1.put("scale", scale5);
		map1.put("rotation", rotation5);
		map1.put("rotationpoint", rotationpoint5);
		map1.put("pointarray", pointarray5);
		map1.put("name", "ee");
		datalist.add(map5);
		
		return datalist;
	}
	
	/**
	 * 显示载入数据的提示信息
	 */
	private void showProgress() {
		if (progress == null) {
			progress = Util.show_process(activity,
					activity.getString(R.string.loading_data), false);
		}
		progress.show();
	}

	/**
	 * 隐藏载入数据的提示信息
	 */
	private void hiddenProgress() {
		if (progress != null) {
			progress.cancel();
			progress = null;
		}
	}
}
